/**
 * The HomecareWorker class represents a homecareworker in the Elderhealth
 * system
 *
 * @author Vy Bui
 * @version 4/13/2007
 */

package com.elderhealth.worker;

import java.sql.*;
import com.elderhealth.schedule.Schedule;
import com.elderhealth.util.DBManager;
import com.elderhealth.util.Log;

public class HomecareWorker
{
    /* instance variables */
    private int homecareWorkerId;
    private String firstName;
    private String lastName;
    public static final String blankNameInput = " ";

    /**
     * Constructor for objects of class HomecareWorker
     */

    public HomecareWorker()
    {
      /* initialise instance variables */
      homecareWorkerId = -1;
      firstName = "";
      lastName = "";
    }

    public HomecareWorker(int id, String fName, String lName)
    {
      /* initialise instance variables */
      homecareWorkerId = id;
      firstName = fName;
      lastName = lName;
    }

    /* Static Method Section */

    /**
     * getWorkerById retrieves a single HomecareWorker by their unique id
     * number
     *
     * @param  identifier   worker id to search on
     * @return     Worker
     */
    public static HomecareWorker getWorkerById (int identifier)
    {
      /* Initialize Variables */
      HomecareWorker foundWorker = null;
      DBManager db = DBManager.getInstance();
      Log logFile = Log.getInstance();

      /* Check inputs to make sure they're valid, if not, make them */
      if (identifier <= 0)
      {
        logFile.error("HomecareWorker: getWorkerById: Identifier " +
            "passed to method was invalid.");
        return foundWorker;
      }

      try
      {
        /* Make query to DB to retrieve workers */
        logFile.debug("HomecareWorker: getWorkerById: Searching for worker" +
            "with identifier: '" + identifier + "'");
        foundWorker = db.getWorkerById(identifier);
        logFile.debug("HomecareWorker: getWorkerById: Completed search.");
        if (null != foundWorker) logFile.debug("HomecareWorker: " +
            "getClientById: HomecareWorker found.");
        /* Return list of Clients */
        return foundWorker;
      }
      catch (Exception e)
      {
        logFile.error("HomecareWorker: getWorkerById: An exception " +
            "occurred. Exception is: " + e.toString());
        return foundWorker;
      }
    }

    /**
     * getWorkerByMatch retrieves a list of matching workers
     * from the database based on the worker's first name and last name
     *
     * @param  lastName   homecareworker last name to search on
     *         firName    womecareworker first name to search on
     * @return     HomecareWorker[]
     */
    public static HomecareWorker[] getWorkerByMatch (String firstName,
                                                     String lastName)
    {
      HomecareWorker[] workers = null;
      DBManager db = DBManager.getInstance();
      Log logFile = Log.getInstance();

      /* Check inputs to make sure they're valid, if not, make them */
      if (null == firstName || firstName.equals(""))
      {
        firstName = blankNameInput;
      }
      if (null == lastName || lastName.equals(""))
      {
        lastName = blankNameInput;
      }

      try
      {
        /* Make query to DB to retrieve homecareworkers */
        logFile.debug("HomecareWorker: getWorkerByMatch: Searching for " +
            "workers with first name: '" + firstName + "' and last name: '" +
            lastName + "'");

        workers = db.getWorkersByName(firstName, lastName);
        logFile.debug("HomecareWorker: getWorkerByMatch: After" +
          "searching for workers.");
        if (null != workers) logFile.debug("HomecareWorker: " +
            "getWorkerByMatch: Workerss found. There were " + workers.length +
            " found.");

        /* Return list of workers */
        return workers;
      }
      catch (Exception e)
      {
        logFile.error("HomecareWorker: getWorkerByMatch: An " +
            "exception occurred. Exception is: " + e.toString());
        return workers;
      }
    }

    /* Method Section */

    /**
     * getSchedule retrieves this HomecareWorker's schedule
     *
     * @param  startTime   start timestamp of schedule to retrieve
     * @param  endTime     end timestamp of schedule to retrieve
     * @return     Schedule
     */

    public Schedule getSchedule(Timestamp startTime, Timestamp endTime)
    {
      Schedule schedule = null;
      DBManager db = DBManager.getInstance();
      Log logFile = Log.getInstance();

      try
      {
        /* Make query to DB to retrieve schedule */
        schedule = db.getWorkerSchedule(homecareWorkerId, startTime, endTime);
        if (null != schedule)
        {
          logFile.debug("HomecareWorker: " +
            "getSchedule: After retrieving schedule.");
        }
        else
        {
          logFile.debug("HomecareWorker: " +
            "getSchedule: No schedule was returned, check earlier entries.");
        }

        /* Return schedule */
        return schedule;
      }
      catch (Exception e)
      {
        logFile.error("HomecareWorker: getSchedule: An " +
            "exception occurred. Exception is: " + e.toString());
        return schedule;
      }
    }

    /* Accessor Methods */

    /**
     * getWorkerId retrieves this worker's ID number
     *
     * @return     homecareWorkerId int
     */

    public int getWorkerId ()
    {
      return homecareWorkerId;
    }

    /**
     * getWorkerName retrieves this worker's name
     *
     * @return     name String
     */

    public String getWorkerName ()
    {
      return firstName + " " + lastName;
    }

    /**
     * getFirstName retrieves this worker's first name
     *
     * @return     firstName String
     */
    public String getFirstName()
    {
      return firstName;
    }

    /**
     * getLastName retrieves this worker's last name
     *
     * @return     lastName String
     */
    public String getLastName()
    {
      return lastName;
    }
}
